package com.test.sort;

import java.util.Arrays;

/**
 * @author: 蒿雨
 * @create: 2022-02-14 11:42
 * @description:插入排序法(比前面排序法)
 */
public class InsertSort {
    public static void main(String[] args) {
        //定义变量
        int[] arr = {101, 34, 119, 1};
        //数组排序
        insertSort(arr);
    }

    //插入排序
    private static void insertSort(int[] arr) {
        for (int i = 1; i <arr.length; i++) {
            int insertVal = arr[i];
            int insertIndex = i-1;
            while (insertIndex>=0&&insertVal<arr[insertIndex]){
                arr[insertIndex+1]=arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex+1]= insertVal;
        }
        System.out.print("排序后的数组："+ Arrays.toString(arr));

    }
}
